package com.aisile.crowd_funding.system.service.impl;

import com.aisile.crowd_funding.system.mapper.SysRoleMapper;
import com.aisile.crowd_funding.system.mapper.SysRoleMenuMapper;
import com.aisile.crowd_funding.system.mapper.SysUserRoleMapper;
import com.aisile.crowd_funding.system.pojo.SysRole;
import com.aisile.crowd_funding.system.pojo.SysRoleExample;
import com.aisile.crowd_funding.system.pojo.SysRoleMenu;
import com.aisile.crowd_funding.system.pojo.SysRoleMenuExample;
import com.aisile.crowd_funding.system.pojo.SysUserRoleExample;
import com.aisile.crowd_funding.system.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ImRoleService implements RoleService {
    @Autowired
    SysRoleMapper sysRoleMapper;

    @Autowired
    SysRoleMenuMapper sysRoleMenuMapper;
    @Autowired
    SysUserRoleMapper sysUserRoleMapper;
    @Override
    public int deleteByPrimaryKey(Long id) {
        int i=sysRoleMapper.deleteByPrimaryKey(id);
        SysRoleMenuExample example=new SysRoleMenuExample();
        SysRoleMenuExample.Criteria criteria = example.createCriteria();
        criteria.andRoleIdEqualTo(id);
        SysUserRoleExample example1=new SysUserRoleExample();
        example.createCriteria().andRoleIdEqualTo(id);
        sysUserRoleMapper.deleteByExample(example1);
        sysRoleMenuMapper.deleteByExample(example);

        return i;
    }

    @Override
    public int deleteByids(Long[] ids) {
          int d=0;
        for(Long i:ids){
            d=sysRoleMapper.deleteByPrimaryKey(i);
        }
        for(Long id:ids){
            SysRoleMenuExample example=new SysRoleMenuExample();
            SysRoleMenuExample.Criteria criteria = example.createCriteria();
            criteria.andRoleIdEqualTo(id);
            SysUserRoleExample example1=new SysUserRoleExample();
            example.createCriteria().andRoleIdEqualTo(id);
            sysUserRoleMapper.deleteByExample(example1);
            sysRoleMenuMapper.deleteByExample(example);
        }
        return d;
    }

    @Override
    public int insert(SysRole record) {
        int i=sysRoleMapper.insert(record);
        Long roleId=record.getRoleId();
        Long[] menuIds=record.getMenuIds();
        for(Long menuId:menuIds){
            SysRoleMenu sysRoleMenu=new SysRoleMenu();
            sysRoleMenu.setMenuId(menuId);
            sysRoleMenu.setRoleId(roleId);
            sysRoleMenuMapper.insert(sysRoleMenu);

        }


        return i;
    }

    @Override
    public SysRole selectByPrimaryKey(Long id) {


        return sysRoleMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKey(SysRole sysRole) {
        int i=sysRoleMapper.updateByPrimaryKey(sysRole);
        SysRoleMenuExample example=new SysRoleMenuExample();
        example.createCriteria().andRoleIdEqualTo(sysRole.getRoleId());
       Long[] s=sysRole.getMenuIds();
       SysRoleMenu sysRoleMenu=new SysRoleMenu();
       for(Long y:s){
           sysRoleMenu.setRoleId(sysRole.getRoleId());
           sysRoleMenu.setMenuId(y);
       }
       sysRoleMenuMapper.insert(sysRoleMenu);

        return i;
    }

    @Override
    public List<SysRole> selectByExample(SysRoleExample sysRoleExample) {
        return sysRoleMapper.selectByExample(null);
    }
}
